
; (function (w, doc) {
    class Swiper {
        constructor() {
            this.animaTime = 300;
            this.duration = 3000;
            this.total = 0;
            this.blockWidth = 0;
            this.playTimer = null;
            this.moveRatio = 0.25;
            this.currentIndex = 1;
            this.init()
        }
        init() {
            this.handleDom();
            this.initEvent();
            this.startTime()
        }
        handleDom() {
            this.swiperBox = doc.querySelector('.swiper-wrap');
            this.swiperEl = this.swiperBox.querySelector('.swiper');
            this.blockWidth = this.swiperBox.offsetWidth;
            const lis = this.swiperEl.querySelectorAll('li');
            this.total = lis.length;
            const first = lis[0].cloneNode(true);
            const last = lis[this.total - 1].cloneNode(true);
            this.swiperEl.insertBefore(last, lis[0]);
            this.swiperEl.appendChild(first);
            this.swiperElStyle = this.swiperEl.style

        }
        initEvent() { }
        startTime() {
            this.playTimer = setInterval(() => {
                this.scrolling = true
                this.currentIndex++;
                this.scrollContent(-this.currentIndex * this.blockWidth);
                this.checkPosition();
                this.scrolling = false
            }, this.duration);
        }
        scrollContent(position) {
            this.swiperElStyle.transition = `transform  ${this.animaTime}ms`;
            this.setTransform(position);
        }
        setTransform(position) {
            this.swiperElStyle.transform = `translateX(${position}px)`;
        }
        stopTimer() {
            clearInterval(this.playTimer)
        }
        checkPosition() {
            setTimeout(() => {
                this.swiperElStyle.transition = '0ms';
                if (this.currentIndex >= this.total + 1) {
                    this.currentIndex = 1;
                    this.setTransform(-this.currentIndex * this.blockWidth);
                } else if (this.currentIndex <= 0) {
                    this.currentIndex = this.total;
                    this.setTransform(-this.currentIndex * this.blockWidth)
                }
            }, this.animaTime)
        }
        changeItem(num) {
            if (this.scrolling) {
                return
            }
            this.stopTimer();
            this.currentIndex += num;
            this.scrollContent(-this.currentIndex * this.blockWidth);
            this.checkPosition();
            this.startTime();
        }
        mouseDown(e) {
            if (this.scrolling) {
                return
            }
            this.stopTimer();
            this.moveing = true;
            this.startX = e.clientX;

        }
        mouseMove(e) {
            if (this.moveing) {
                const currentX = e.clientX;
                this.distance = currentX - this.startX;
                const moveDistance = -this.currentIndex * this.blockWidth + this.distance;
                this.setTransform(moveDistance);
            }
        }
        mouseUp(e) {
            if (!this.moveing) {
                return
            }
            const res = Math.abs(this.distance) > this.blockWidth * this.moveRatio;

            if (this.distance === 0) {
                return
            }
            if (this.distance > 0 && res) {
                this.currentIndex--
            } else if (this.distance < 0 && res) {
                this.currentIndex++
            }
            this.scrollContent(-this.currentIndex * this.blockWidth);
            this.checkPosition();
            this.startTimer()
        }
    }
})(window, document)